package com.leecode;

import com.test.TreeNode;

public class Leet538 {
	public static void main(String[] args) {
		new Leet538().convertBST(new TreeNode(2, new TreeNode(1), new TreeNode(3)));
	}
	public TreeNode convertBST(TreeNode root) {
		updateNode(root);
		return root;
	}
	public int updateNode(TreeNode<Integer> root) {
		if(root==null)return 0;

		if(updateNode(root.right)==0){
			if(updateNode(root.left)==0){
				return root.val;
			}else{
				root.left.val+=root.val;
			}
		}else{
			root.val+=updateNode(root.right);
			root.left.val+=root.val;
		}
		return 0;
	}
}
